package service;

import com.fr.base.FRContext;
import com.fr.script.AbstractFunction;
import com.fr.stable.Primitive;
import com.fr.stable.StringUtils;
import dao.JDBCConn;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Level;

import static util.ConstantSetUp.passWord;
import static util.ConstantSetUp.url;
import static util.ConstantSetUp.user;

/**
 * 对已存在的项目值查询。查询时间我最近的document内的值
 * Created by Xtra on 3/30 0030.
 *
 * @author Xtra
 */
public class ExistItemQuery extends AbstractFunction {

    /**
     * 获取前台界面参数：
     * 1.documentID 文档编号
     * 2.displayTitle 项目名称
     *
     * @param objects
     * @return content  项目值
     */
    @Override
    public Object run(Object[] objects) {
        String result = "";
        try {
            String patientID = (String) objects[0];
            String templateCode = (String) objects[1];
            String documentID = (String) objects[2];
            String displayTitle = (String) objects[3];
            if (!StringUtils.isEmpty(documentID) || !StringUtils.isEmpty(displayTitle)) {
                ItemQuery itemQuery = new ItemQuery();
                Object[] objects1 = {documentID, displayTitle};
                String res = (String) itemQuery.run(objects1);
                if (res != null || !"".equals(res)) {
                    if (!StringUtils.isEmpty(patientID) || !StringUtils.isEmpty(displayTitle)) {
                        String sql1 = "select c_content  from dbo.t_mb_emr_docValue where c_document = (" +
                                "select c_uuid from dbo.t_mb_emr_doc " +
                                "where c_createDate= (select MAX(c_createDate) from dbo.t_mb_emr_doc " +
                                "where c_patientID = '" + patientID + "' and c_template = '" + templateCode + "')) and c_item = (" +
                                "select c_itemID from dbo.t_mb_emr_items where  c_template ='" + templateCode + "' and c_displayTitle = '" + displayTitle + "')";

                        JDBCConn jdbcConn = new JDBCConn();
                        FRContext.getLogger().log(Level.INFO, "链接数据库192.168.0.60中：---{数据库url:" + url + ",登录名：nurse}!");
                        jdbcConn.getConnection(url, user, passWord);
                        FRContext.getLogger().log(Level.INFO, "准备执行SQL语句：**{" + sql1 + "}!");
                        result = jdbcConn.executeQuery(sql1);
                        jdbcConn.close();

                    } else {
                        FRContext.getLogger().log(Level.WARNING, "SQL执行异常！请检查数据或插件！插件名：plugin-itemQuery");
                        return Primitive.ERROR_VALUE;
                    }
                } else {
                    result = res;
                }
            }
        } catch (Exception e) {
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
            FRContext.getLogger().log(Level.WARNING, stringWriter.toString());
        }
        FRContext.getLogger().log(Level.INFO, "SQL执行完毕！输出结果！");
        return result;
    }
}
